/*     */ package com.qmsk.business.sys.basic;
/*     */ 
/*     */ import com.utils.basic.CFileComm;
/*     */ import com.utils.file.GlobalParams;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UploadFileUtil
/*     */ {
/*  27 */   private static final Logger logger = Logger.getLogger(UploadFileUtil.class);
/*     */   public static final String TEMP_DIRECTORY = "/uploadfile/temp";
/*     */   public static final String REAL_DIRECTORY = "/uploadfile/real";
/*     */   private static final int SIZE_THRESHOLD = 1024;
/*     */   private static final int SIZE_MAX = 104857600;
/*  35 */   private static String aparchURL = null;
/*  36 */   private static String aparchRoot = null;
/*     */ 
/*     */   public static Map<String, String> uploadFile(HttpServletRequest request, String filePath, boolean isChangeFileName)
/*     */   {
/*  51 */     if ((aparchRoot == null) || (aparchURL == null)) {
/*  52 */       aparchRoot = GlobalParams.getGlobalParams().getApacheRoot();
/*  53 */       aparchURL = GlobalParams.getGlobalParams().getApacheUrl();
/*     */     }
/*     */ 
/*  56 */     HttpSession session = request.getSession(false);
/*  57 */     if (session == null) {
/*  58 */       session = request.getSession(true);
/*     */     }
/*     */ 
/*  61 */     ServletContext app = session.getServletContext();
/*     */ 
/*  63 */     String realDir = aparchRoot + filePath;
/*  64 */     String tempDir = app.getRealPath("/uploadfile/temp") + File.separator;
/*     */ 
/*  66 */     File repository = new File(tempDir);
/*     */ 
/*  68 */     DiskFileItemFactory factory = new DiskFileItemFactory(1024, 
/*  69 */       repository);
/*     */ 
/*  71 */     ServletFileUpload fileUpload = new ServletFileUpload(factory);
/*  72 */     fileUpload.setSizeMax(104857600L);
/*     */     try
/*     */     {
/*  75 */       Map result = new HashMap();
/*     */ 
/*  77 */       List fileItems = fileUpload.parseRequest(request);
/*  78 */       Iterator iterator = fileItems.iterator();
/*  79 */       while (iterator.hasNext()) {
/*  80 */         FileItem fileItem = (FileItem)iterator.next();
/*     */ 
/*  82 */         boolean isFormField = fileItem.isFormField();
/*  83 */         if (isFormField) {
/*  84 */           result.put(fileItem.getFieldName(), 
/*  85 */             fileItem.getString("UTF-8"));
/*  86 */           logger.info("FormField-Name:" + fileItem.getFieldName() + 
/*  87 */             "; FormField-Value:" + 
/*  88 */             fileItem.getString("UTF-8"));
/*     */         }
/*     */         else {
/*  91 */           String fileName = null;
/*     */ 
/*  93 */           if (isChangeFileName)
/*  94 */             fileName = fileItem.getFieldName() + 
/*  95 */               CFileComm.getExtName(fileItem.getName());
/*     */           else {
/*  97 */             fileName = fileItem.getName();
/*     */           }
/*     */ 
/* 100 */           File dir = new File(realDir);
/* 101 */           if (!(dir.exists())) {
/* 102 */             dir.mkdirs();
/*     */           }
/*     */ 
/* 105 */           File realFile = new File(realDir + fileName);
/* 106 */           fileItem.write(realFile);
/*     */ 
/* 108 */           logger.info("File Upload Success:" + fileItem.getName() + 
/* 109 */             "; New File:" + fileName + "; File Size:" + 
/* 110 */             fileItem.getSize());
/*     */ 
/* 112 */           String srcURL = aparchURL + filePath + fileName;
/* 113 */           logger.info("SrcURL:" + srcURL);
/*     */ 
/* 115 */           result.put(fileItem.getFieldName(), srcURL);
/*     */         }
/*     */       }
/* 118 */       return result;
/*     */     } catch (Exception e) {
/* 120 */       logger.info(e.getMessage());
/*     */     }
/* 122 */     return null;
/*     */   }
/*     */ 
/*     */   public static Map<String, String> uploadImage(HttpServletRequest request, String filePath, boolean isChangeFileName)
/*     */   {
/* 139 */     HttpSession session = request.getSession(false);
/* 140 */     if (session == null) {
/* 141 */       session = request.getSession(true);
/*     */     }
/*     */ 
/* 144 */     ServletContext app = session.getServletContext();
/*     */ 
/* 146 */     String realDir = filePath;
/* 147 */     String tempDir = app.getRealPath("/uploadfile/temp") + File.separator;
/*     */ 
/* 149 */     File repository = new File(tempDir);
/*     */ 
/* 151 */     DiskFileItemFactory factory = new DiskFileItemFactory(1024, 
/* 152 */       repository);
/*     */ 
/* 154 */     ServletFileUpload fileUpload = new ServletFileUpload(factory);
/* 155 */     fileUpload.setSizeMax(104857600L);
/*     */     try
/*     */     {
/* 158 */       Map result = new HashMap();
/*     */ 
/* 160 */       List fileItems = fileUpload.parseRequest(request);
/* 161 */       Iterator iterator = fileItems.iterator();
/* 162 */       while (iterator.hasNext()) {
/* 163 */         FileItem fileItem = (FileItem)iterator.next();
/*     */ 
/* 165 */         boolean isFormField = fileItem.isFormField();
/* 166 */         if (isFormField) {
/* 167 */           result.put(fileItem.getFieldName(), 
/* 168 */             fileItem.getString("UTF-8"));
/* 169 */           logger.info("FormField-Name:" + fileItem.getFieldName() + 
/* 170 */             "; FormField-Value:" + 
/* 171 */             fileItem.getString("UTF-8"));
/*     */         }
/*     */         else {
/* 174 */           String fileName = null;
/*     */ 
/* 176 */           if (isChangeFileName)
/* 177 */             fileName = fileItem.getFieldName() + 
/* 178 */               CFileComm.getExtName(fileItem.getName());
/*     */           else {
/* 180 */             fileName = fileItem.getName();
/*     */           }
/*     */ 
/* 183 */           File dir = new File(realDir);
/* 184 */           if (!(dir.exists())) {
/* 185 */             dir.mkdirs();
/*     */           }
/*     */ 
/* 188 */           File realFile = new File(realDir + fileName);
/* 189 */           fileItem.write(realFile);
/*     */ 
/* 191 */           logger.info("File Upload Success:" + fileItem.getName() + 
/* 192 */             "; New File:" + fileName + "; File Size:" + 
/* 193 */             fileItem.getSize());
/*     */ 
/* 195 */           String srcURL = fileName;
/* 196 */           logger.info("SrcURL:" + srcURL);
/*     */ 
/* 198 */           result.put(fileItem.getFieldName(), srcURL);
/*     */         }
/*     */       }
/* 201 */       return result;
/*     */     } catch (Exception e) {
/* 203 */       logger.info(e.getMessage());
/*     */     }
/* 205 */     return null;
/*     */   }
/*     */ 
/*     */   public static void uploadFileOfRequest(HttpServletRequest request, HttpServletResponse response, String sDir, List<String> listFileName, String appointName)
/*     */     throws ServletException, IOException
/*     */   {
/* 213 */     request.setCharacterEncoding("UTF-8");
/* 214 */     response.setContentType("text/html;charset=UTF-8");
/*     */ 
/* 218 */     DiskFileItemFactory factory = new DiskFileItemFactory();
/* 219 */     factory.setSizeThreshold(1024);
/* 220 */     ServletFileUpload upload = new ServletFileUpload(factory);
/* 221 */     upload.setSizeMax(104857600L);
/*     */     try
/*     */     {
/* 224 */       List fileItems = upload.parseRequest(request);
/* 225 */       Iterator iter = fileItems.iterator();
/* 226 */       while (iter.hasNext()) {
/* 227 */         FileItem item = (FileItem)iter.next();
/* 228 */         if (item.isFormField())
/*     */           continue;
/* 230 */         String name = item.getName();
/* 231 */         logger.info("item: " + name);
/* 232 */         logger.info("appointName: " + appointName);
/* 233 */         String extName = name.substring(name.lastIndexOf("."));
/* 234 */         String saveFileName = appointName + extName;
/* 235 */         if (listFileName != null) {
/* 236 */           listFileName.add(saveFileName);
/*     */         }
/*     */         try
/*     */         {
/* 240 */           logger.info("upload dir:" + sDir);
/* 241 */           File filePath = new File(sDir);
/* 242 */           if (!(filePath.exists())) {
/* 243 */             logger.info("dir isn't exists:" + sDir);
/* 244 */             filePath.mkdirs();
/*     */ 
/* 246 */             CFileComm.setExecutablePermission(filePath);
/*     */           } else {
/* 248 */             logger.info("dir is exists:" + sDir);
/*     */           }
/* 250 */           File fileItem = new File(sDir + saveFileName);
/* 251 */           logger.info("sDir_item: " + sDir + saveFileName);
/* 252 */           item.write(fileItem);
/*     */ 
/* 254 */           CFileComm.setExecutablePermission(fileItem);
/*     */         } catch (Exception e) {
/* 256 */           logger.error(e.getMessage());
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (FileUploadException e) {
/* 261 */       logger.error(e.getMessage());
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\新建文件夹\WEB-INF\classes\
 * Qualified Name:     com.qmsk.business.sys.basic.UploadFileUtil
 * JD-Core Version:    0.5.3
 */